package cglgu.trellis.chado.das2.cglgu.registry;

/*
 * $Id: RegistryManager.java 191 2011-03-27 00:38:22Z sundaram.georgetown@gmail.com $
 * $Author: sundaram.georgetown@gmail.com $
 * $Date: 2011-03-27 00:38:22 +0000 (Sun, 27 Mar 2011) $
 * $Rev: 191 $
 * $HeadURL: http://genomancer.googlecode.com/svn/branches/gu-dev-branch/tengcha/src/cglgu/trellis/chado/das2/cglgu/registry/RegistryManager.java $
 */

import cglgu.trellis.chado.das2.cglgu.Project;

/*  
    This is the default RegistryManager.

 */

public class RegistryManager {

	private List<String> projectNameList; /* List of project names */
	private List<Project> projectList; /* List of projects */
	private HashMap masterProjectHash; /* HashMap key: project name value: project */
	private int projectCtr=0; /* Keep track of number of projects */

	private static RegistryManager instance = null;

	public static RegistryManager getInstance(){

		if (instance == null){
			instance = new RegistryManager();
		}

		return instance;
	}

	protected RegistryManager(){

		masterProjectHash = new HashMap<String, Project>();
		loadWaspProject();

	}

	public List<String> getProjectNameList(){
		return projectNameList;
	}

	public List<Project> getProjectList(){
		return projectList;
	}
	
	private void loadWaspProject(){

		String projectName = "wasp";

		Project project = new Project("wasp","postgresql","gog","1.0","chado_guest","chado_guest");

		// Add the wasp project to the master lookup
		masterProjectHash.put(projectName, project); 
		projectNameList.add(projectName);
		projectList.add(project);
		projectCtr++;
	}


	public HashMap<String, String> getProject(String projectName){

		if (projectName == null){
			System.err.println("projectName was null");
			System.exit(1);
		}

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does not exist in the projectHash");
			System.exit(1);
		}

		Project project = masterProjectHash.get(projectName);

		return project;
	}

	public boolean addProject(String projectName, String database, String vendor, String server, String assemblyVersion, String username, String password){

		if (masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName already exists in the masterProjectHash");
			return false;
		}

		Project project = new Project(database, vendor, server, assemblyVersion, username, password);

		// Add this project to the master lookup
		masterProjectHash.put(projectName, project); 
		projectNameList.add(projectName);
		projectList.add(project);
		projectCtr++;

		return true;
	}

	public boolean hasProject(String projectName){

		if (masterProjectHash.containsKey(projectName)){
			return true;
		}

		return false;
	}

	public int getProjectCount(){
		return projectCtr;
	}


	public boolean setDatabase(String projectName, String database){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		if (! project.setDatabase(database)){
			System.err.println("was not able to set database for project " + projectName);
			return false;
		}

		return true;
	}

	public String getDatabase(String projectName){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		String database = project.getDatabase();

		return database;
	}


	public boolean setVendor(String projectName, String vendor){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		if (! project.setVendor(vendor)){
			System.err.println("was not able to set vendor for project " + projectName);
			return false;
		}

		return true;
	}

	public String getVendor(String projectName){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		String vendor = project.getVendor();

		return vendor;
	}

	public boolean setServer(String projectName, String server){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		if (! project.setServer(server)){
			System.err.println("was not able to set server for project " + projectName);
			return false;
		}

		return true;
	}

	public String getServer(String projectName){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		String server = project.getServer();

		return server;
	}


	public boolean setAssemblyVersion(String projectName, String assemblyVersion){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		if (! project.setAssemblyVersion(assemblyVersion)){
			System.err.println("was not able to set assemblyVersion for project " + projectName);
			return false;
		}

		return true;
	}

	public String getAssemblyVersion(String projectName){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		String assemblyVersion = project.getAssemblyVersion();

		return assemblyVersion;
	}


	public boolean setUsername(String projectName, String username){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		if (! project.setUsername(username)){
			System.err.println("was not able to set username for project " + projectName);
			return false;
		}

		return true;
	}

	public String getUsername(String projectName){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		String username = project.getUsername();

		return username;
	}


	public boolean setPassword(String projectName, String password){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		if (! project.setPassword(password)){
			System.err.println("was not able to set password for project " + projectName);
			return false;
		}

		return true;
	}

	public String getPassword(String projectName){

		if (! masterProjectHash.containsKey(projectName)){
			System.err.println(projectName + " projectName does exist in the masterProjectHash!");
			return null;
		}

		Project project = masterProjectHash.get(projectName);

		String password = project.getPassword();

		return password;
	}




}